package web.reclamos.dao.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import web.reclamos.beans.Area;
import web.reclamos.dao.AreaDao;
import web.reclamos.dao.exception.ExceptionReclamos;

public class AreaDaoSql  implements AreaDao{

    public void crear(Area entidad) throws ExceptionReclamos {
       Connection cn = null;
        PreparedStatement pstm = null;
        try{
            cn = ConexionDB.getInstance().getConnection();
            String sql = "insert into area (id_area, nombre_area, id_empresa, id_coorporacion) values (?,?,?,?)";
            pstm = cn.prepareStatement(sql);
            pstm.setInt(1, entidad.getId_area());
            pstm.setString(2, entidad.getNombre_area());
            pstm.setInt(3, entidad.getEmpresa().getId_empresa());
            pstm.setInt(4, entidad.getCoorporacion().getId_coorporacion());
            pstm.executeUpdate();
        }catch(SQLException ex){
            System.out.println(ex);
        }catch(Exception ex){
              System.out.println(ex);
        }finally{
            ConexionDB.getInstance().close(pstm);
            ConexionDB.getInstance().close(cn);
        }
    }

    public void modificar(Area entidad) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void eliminar(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Area traerPorId(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Area> traerTodo() {
        Connection cn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Area> areas = new ArrayList<Area>();
        try{
            cn = ConexionDB.getInstance().getConnection();
            String sql = "select * from area";
            pstm = cn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next()){
                areas.add(new Area(rs.getInt("id_area"),
                                         rs.getString("nombre_area")
                             ));
            }
        }catch(SQLException ex){
            System.out.println(ex);
        }catch(Exception ex){
              System.out.println(ex);
        }finally{
            ConexionDB.getInstance().close(rs);
            ConexionDB.getInstance().close(pstm);
            ConexionDB.getInstance().close(cn);
        }
        return areas;
    }

    public List<Area> traerPorNombre(String nombre) {
         Connection cn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Area> areas = new ArrayList<Area>();
        try{
            cn = ConexionDB.getInstance().getConnection();
            String sql = "select * from area where nombre_area like ?";
            pstm = cn.prepareStatement(sql);
            pstm.setString(1, nombre + "%");
            rs = pstm.executeQuery();
            while(rs.next()){
                areas.add(new Area(rs.getInt("id_area"),
                                         rs.getString("nombre_area")
                             ));
            }
        }catch(SQLException ex){
            System.out.println(ex);
        }catch(Exception ex){
              System.out.println(ex);
        }finally{
            ConexionDB.getInstance().close(rs);
            ConexionDB.getInstance().close(pstm);
            ConexionDB.getInstance().close(cn);
        }
        return areas;
    }

}
